//
//  DiaryDetialViewController.m
//  Lovely Food
//
//  Created by 李经纬 on 15/12/8.
//  Copyright © 2015年 李经纬. All rights reserved.
//

#import "DiaryDetialViewController.h"
#import "DiaryModel.h"
#import "DiaryDetialViewController.h"
#import "DiaryDetailCell.h"
#import "DiaryDetailModel.h"
#import "UIImageView+WebCache.h"
@interface DiaryDetialViewController ()
@property (nonatomic, strong)NSMutableArray * diaryDetailArray;
@property (nonatomic, strong)NSArray * urlArray;
@end

@implementation DiaryDetialViewController
-(NSMutableArray *)diaryDetailArray
{
    if (_diaryDetailArray == nil) {
        _diaryDetailArray = [NSMutableArray array];
        
    }
    return _diaryDetailArray;
}

-(NSArray *)urlArray
{
    if (_urlArray == nil) {
        _urlArray = [NSArray array];
    }
    return _urlArray;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    [self.tableView registerClass:[DiaryDetailCell class] forCellReuseIdentifier:@"Cell"];
    
    [self loadData];
}

-(void)loadData
{
    NSURLSession * session = [NSURLSession sharedSession];
    
    // //拼接id成功 signature失败------>放弃这个方法 detailID变成rowOfCell记录点击的是第几行 然后解析对应的url
    //    NSString * s = @"http://www.youyuwei.com/api/note?app_code=com.youyuwei.yuwei.ios&hardware_model=iPhone5%2C2&app_ver=2.1&sys_ver=9.1&device_type=ios&ywsdk_ver=20131018&open_udid=086035be98bdf57eb36141a1a8b4c010171912b6&start=0&oauth_timestamp=1449456873&device_token=5fedbc3e6bc4a9aa4223bc304eaaf624a464b0cc3222ba15a80106126ed7cf45&x_auth_mode=client_auth&oauth_signature_method=HMAC-SHA1&id=";
    //    NSString * s2 = @"&ver=2&uid=18080&idfa=73BEDD47-7F7A-4DCC-8F35-15111C2264FE&channel_id=App%20Store&oauth_token=18080_ebbf4985ba91d7e9ed8fecf6105868c7&oauth_consumer_key=4&oauth_version=1.0&oauth_signature=ISyqnjlKw2eiB6MprGYewq1fcVE%3D&debug=0&oauth_nonce=d50385f76ac554494628638874c21564";
    //
    //    NSString *str = [NSString stringWithFormat:@"%@%@%@", s,self.detailID, s2];
    NSString * s0 = @"http://www.youyuwei.com/api/note?app_code=com.youyuwei.yuwei.ios&hardware_model=iPhone5%2C2&app_ver=2.1&sys_ver=9.1&device_type=ios&ywsdk_ver=20131018&open_udid=086035be98bdf57eb36141a1a8b4c010171912b6&start=0&oauth_timestamp=1449556709&device_token=5fedbc3e6bc4a9aa4223bc304eaaf624a464b0cc3222ba15a80106126ed7cf45&x_auth_mode=client_auth&oauth_signature_method=HMAC-SHA1&id=11249&ver=2&uid=18080&idfa=73BEDD47-7F7A-4DCC-8F35-15111C2264FE&channel_id=App%20Store&oauth_token=18080_ebbf4985ba91d7e9ed8fecf6105868c7&oauth_consumer_key=4&oauth_version=1.0&oauth_signature=MdH8PZLQ%2BBFOuZtqVM4GD0G%2BZgE%3D&debug=0&oauth_nonce=b3f74e35ac5c1a941cbc42b96d07ff7e";
    NSString * s1 = @"http://www.youyuwei.com/api/note?app_code=com.youyuwei.yuwei.ios&hardware_model=iPhone5%2C2&app_ver=2.1&sys_ver=9.1&device_type=ios&ywsdk_ver=20131018&open_udid=086035be98bdf57eb36141a1a8b4c010171912b6&start=0&oauth_timestamp=1449556714&device_token=5fedbc3e6bc4a9aa4223bc304eaaf624a464b0cc3222ba15a80106126ed7cf45&x_auth_mode=client_auth&oauth_signature_method=HMAC-SHA1&id=11216&ver=2&uid=18080&idfa=73BEDD47-7F7A-4DCC-8F35-15111C2264FE&channel_id=App%20Store&oauth_token=18080_ebbf4985ba91d7e9ed8fecf6105868c7&oauth_consumer_key=4&oauth_version=1.0&oauth_signature=ktLKVx7tzSZIwni3%2BD%2Fbc0Es3U8%3D&debug=0&oauth_nonce=d94697b711757d197a7a053b12283536";
    NSString * s2 = @"http://www.youyuwei.com/api/note?app_code=com.youyuwei.yuwei.ios&hardware_model=iPhone5%2C2&app_ver=2.1&sys_ver=9.1&device_type=ios&ywsdk_ver=20131018&open_udid=086035be98bdf57eb36141a1a8b4c010171912b6&start=0&oauth_timestamp=1449556717&device_token=5fedbc3e6bc4a9aa4223bc304eaaf624a464b0cc3222ba15a80106126ed7cf45&x_auth_mode=client_auth&oauth_signature_method=HMAC-SHA1&id=10153&ver=2&uid=18080&idfa=73BEDD47-7F7A-4DCC-8F35-15111C2264FE&channel_id=App%20Store&oauth_token=18080_ebbf4985ba91d7e9ed8fecf6105868c7&oauth_consumer_key=4&oauth_version=1.0&oauth_signature=d0Iy12UdMCvQWeV5Ewm68r%2BpVPA%3D&debug=0&oauth_nonce=715f510533da2ad4702f701277c1da50";
    NSString * s3 = @"http://www.youyuwei.com/api/note?app_code=com.youyuwei.yuwei.ios&hardware_model=iPhone5%2C2&app_ver=2.1&sys_ver=9.1&device_type=ios&ywsdk_ver=20131018&open_udid=086035be98bdf57eb36141a1a8b4c010171912b6&start=0&oauth_timestamp=1449556721&device_token=5fedbc3e6bc4a9aa4223bc304eaaf624a464b0cc3222ba15a80106126ed7cf45&x_auth_mode=client_auth&oauth_signature_method=HMAC-SHA1&id=11107&ver=2&uid=18080&idfa=73BEDD47-7F7A-4DCC-8F35-15111C2264FE&channel_id=App%20Store&oauth_token=18080_ebbf4985ba91d7e9ed8fecf6105868c7&oauth_consumer_key=4&oauth_version=1.0&oauth_signature=t0r4FE%2F%2Fi58eHHXaOESlYdaL3IY%3D&debug=0&oauth_nonce=d72a5d19881f25d18e76a09b9dc07ae1";
    
    switch (self.rowOfCell) {
        case 0:{
            NSURL *url = [NSURL URLWithString:s0];
            NSMutableURLRequest * request = [NSMutableURLRequest requestWithURL:url];
            NSURLSessionDataTask * task = [session dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
                
                
                NSDictionary * dic = [NSJSONSerialization JSONObjectWithData:data options:(NSJSONReadingAllowFragments) error:nil];
                NSDictionary * dic1 = dic[@"data"];
                for (NSDictionary * dic2 in dic1[@"content"]) {
                    DiaryDetailModel * m = [[DiaryDetailModel alloc]init];
                    [m setValuesForKeysWithDictionary:dic2];
                    [self.diaryDetailArray addObject:m];
                    
                }
                
                
                
                dispatch_async(dispatch_get_main_queue(), ^{
                    
                    [self.tableView reloadData];
                });
            }];
            [task resume];
            break;
        }
            
            
            
        case 1:{
            
            NSURL *url = [NSURL URLWithString:s1];
            NSMutableURLRequest * request = [NSMutableURLRequest requestWithURL:url];
            NSURLSessionDataTask * task = [session dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
                
                
                NSDictionary * dic = [NSJSONSerialization JSONObjectWithData:data options:(NSJSONReadingAllowFragments) error:nil];
                NSDictionary * dic1 = dic[@"data"];
                for (NSDictionary * dic2 in dic1[@"content"]) {
                    DiaryDetailModel * m = [[DiaryDetailModel alloc]init];
                    [m setValuesForKeysWithDictionary:dic2];
                    [self.diaryDetailArray addObject:m];
                    
                }
                
                
                
                dispatch_async(dispatch_get_main_queue(), ^{
                    
                    [self.tableView reloadData];
                });
            }];
            [task resume];
            break;
        }
        case 2:{
            
            NSURL *url = [NSURL URLWithString:s2];
            NSMutableURLRequest * request = [NSMutableURLRequest requestWithURL:url];
            NSURLSessionDataTask * task = [session dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
                
                
                NSDictionary * dic = [NSJSONSerialization JSONObjectWithData:data options:(NSJSONReadingAllowFragments) error:nil];
                NSDictionary * dic1 = dic[@"data"];
                for (NSDictionary * dic2 in dic1[@"content"]) {
                    DiaryDetailModel * m = [[DiaryDetailModel alloc]init];
                    [m setValuesForKeysWithDictionary:dic2];
                    [self.diaryDetailArray addObject:m];
                    
                }
                
                
                
                dispatch_async(dispatch_get_main_queue(), ^{
                    
                    [self.tableView reloadData];
                });
            }];
            [task resume];
            break;
        }
        case 3:{
            
            NSURL *url = [NSURL URLWithString:s3];
            NSMutableURLRequest * request = [NSMutableURLRequest requestWithURL:url];
            NSURLSessionDataTask * task = [session dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
                
                
                NSDictionary * dic = [NSJSONSerialization JSONObjectWithData:data options:(NSJSONReadingAllowFragments) error:nil];
                NSDictionary * dic1 = dic[@"data"];
                for (NSDictionary * dic2 in dic1[@"content"]) {
                    DiaryDetailModel * m = [[DiaryDetailModel alloc]init];
                    [m setValuesForKeysWithDictionary:dic2];
                    [self.diaryDetailArray addObject:m];
                    
                }
                
                
                
                dispatch_async(dispatch_get_main_queue(), ^{
                    
                    [self.tableView reloadData];
                });
            }];
            [task resume];
            break;
        }
        default:
            break;
    }
    
    
    
    
    
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    //#warning Incomplete implementation, return the number of sections
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    //#warning Incomplete implementation, return the number of rows
    return self.diaryDetailArray.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    DiaryDetailCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
    
    DiaryDetailModel * m = self.diaryDetailArray[indexPath.row];
    cell.textLabels.text = m.value;
    [cell.imageViews sd_setImageWithURL:[NSURL URLWithString:m.value]];
    
    return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    
    //    if () {
    //
    //
    //    }
    // 這裏返回需要的高度
    DiaryDetailModel * m = self.diaryDetailArray[indexPath.row];
    return [DiaryDetailCell cellHeight:[self heightWithString:m.value]];
}


//plancell自适应高度
-(CGFloat)heightWithString:(NSString *)aString
{
    CGRect r =[aString boundingRectWithSize:CGSizeMake(CGRectGetWidth(self.tableView.frame)-10, 2000) options:(NSStringDrawingUsesLineFragmentOrigin) attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:17.0f]} context:nil];
    return  r.size.height+10;
}
/*
 // Override to support conditional editing of the table view.
 - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
 // Return NO if you do not want the specified item to be editable.
 return YES;
 }
 */

/*
 // Override to support editing the table view.
 - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
 if (editingStyle == UITableViewCellEditingStyleDelete) {
 // Delete the row from the data source
 [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
 } else if (editingStyle == UITableViewCellEditingStyleInsert) {
 // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
 }
 }
 */

/*
 // Override to support rearranging the table view.
 - (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
 }
 */

/*
 // Override to support conditional rearranging of the table view.
 - (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
 // Return NO if you do not want the item to be re-orderable.
 return YES;
 }
 */

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
